import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@abp/components';
import {
  EditionServiceProxy,
  EditionListDto,
} from '@abp/service-proxies/service-proxies';
import { CreateOrEditEditionComponent } from './create-or-edit-edition/create-or-edit-edition.component';

@Component({
  templateUrl: './editions.component.html',
  styleUrls: ['./editions.component.less'],
})
export class EditionComponent extends AppComponentBase {
  selectedPermission = '';

  /** 排序 */
  sortValue = null;
  sortKey = null;

  /** 加载 */
  skeletonLoading = true;
  switchloading = false;
  isActiveSwitchLoading = false;

  constructor(injector: Injector, private editionService: EditionServiceProxy) {
    super(injector);
  }

  ngOnInit(): void {
    this.searchData();
  }

  sort(sort: { key: string; value: string }): void {
    this.tableHelper.sort = sort;
    //this.searchData();
  }

  /**
   * 刷新表格数据
   * @param args
   */
  refresh(): void {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    if (reset) {
      this.tableHelper.pageIndex = 1;
    }
    this.tableHelper.isLoading = true;
    this.editionService.getEditions().subscribe(data => {
      this.tableHelper.isLoading = false;
      this.tableHelper.totalRecordsCount = data.items.length;
      this.tableHelper.records = data.items;
    });
  }

  createBuildingPlan(): void {
    let config = this.drawerConfig(
      '创建新的版本',
      CreateOrEditEditionComponent,
      {
        id: undefined,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  editBuildingPlan(data: EditionListDto): void {
    let config = this.drawerConfig(
      '编辑版本:' + data.displayName,
      CreateOrEditEditionComponent,
      {
        id: data.id,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  deleteConfirm(id: number): void {
    this.editionService.deleteEdition(id).subscribe(() => {
      this.searchData();
      this.notify.success('删除成功！');
    });
  }
}
